package com.itheima.service.impl;

<<<<<<< HEAD
import com.github.pagehelper.PageHelper;
import com.itheima.domain.City;
import com.itheima.domain.Product;
import com.itheima.mapper.ProductMapper;
import com.itheima.service.ProductService;
import com.itheima.util.MapperUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class ProductServiceImpl implements ProductService {
    //带分页的查询
    @Override
    public List<Product> findAll(String pageNum, String pageSize) {
        //获取session对象
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        int pageN=1;
        int pageS=10;
        if (StringUtils.isNotBlank(pageNum)){
            pageN= Integer.parseInt(pageNum);
        }if (StringUtils.isNotBlank(pageSize)){
            pageS= Integer.parseInt(pageSize);
        }
        PageHelper.startPage(pageN,pageS);
        List<Product> list=mapper.findAll();
        //将date日期类转化为String类型 封装到product中
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Product product:list) {
            Date departureTime = product.getDepartureTime();
            String format = formatter.format(departureTime);
            product.setDepartureTimeStr(format);
        }
        return list;
    }
   //不带分页的查询
    @Override
    public List<Product> findAll() {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        List<Product> all = mapper.findAll();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Product product:all) {
            Date departureTime = product.getDepartureTime();
            String format = formatter.format(departureTime);
            product.setDepartureTimeStr(format);
        }
        return all;
    }

    @Override
    //新增数据
    public void save(Product product) {
        //获取SqlSession对象
        SqlSession session = MapperUtil.getSession();
        //获取Dao层对象
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        //随机生成id并赋给id
        product.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        //获取城市名称
        String cn = product.getCityName();
       //根据城市名称获取城市对象
        CityServiceImp cs = new CityServiceImp();
        City byName = cs.findByName(cn);
        //获取城市id.将城市id赋给产品对象的CityId
        product.setCityId(byName.getId());
        //调用Dao层save方法
        mapper.save(product);
        //关流
        MapperUtil.close(session);
    }
    @Override
    public void deleteByIds(String ids) {
        //获取的ids进行分割
        String[] id = ids.split(",");
        //获取 SqlSession
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        mapper.deleteByIds(id);
        MapperUtil.close(session);
    }
   //根据id查询
    @Override
    public Product findById(String id) {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
       Product product= mapper.findById(id);
       return product;
    }
    //跟新修改数据
    @Override
    public void update(Product pro) {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        CityServiceImp cs = new CityServiceImp();
        String cn = pro.getCityName();
        City byName = cs.findByName(cn);
        String id = byName.getId();
        pro.setCityId(id);
         mapper.update(pro);
        MapperUtil.close(session);
    }
=======
import com.itheima.service.ProductService;

public class ProductServiceImpl implements ProductService {
>>>>>>> 87a0f68c2b3b7de3b37de1968a6eac4ab534cee5
}
